package passwordapp;

/**
 * Copyright [2011] [Raymond Pankhurst]

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

     http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.

 */

import javax.microedition.lcdui.*;
import java.util.Enumeration;
import javax.microedition.io.file.*;
import javax.microedition.io.*;

/**
 * @author rpankhurst
 */
public class FileBrowserExtended extends List implements CommandListener {
    Display m_Display = null;
    //ChoiceGroup m_Files = new ChoiceGroup("Files", ChoiceGroup.EXCLUSIVE);
    //TextField m_FileName = new TextField("File name", null, 255, TextField.ANY);
    FileConnection m_CurPath = null;
    FileConnection m_SelectedFile = null;
    Command FILE_SELECT_COMMAND = List.SELECT_COMMAND;
    CommandListener commandListener = null;
    //<editor-fold defaultstate="collapsed" desc=" Generated Fields ">//GEN-BEGIN:|fields|0|
    //</editor-fold>//GEN-END:|fields|0|
    /**
     * The FileBrowser2 constructor.
     * @param title Title to display 
     * @param save display as a save dialog
     */
    public FileBrowserExtended() {
        super("", List.IMPLICIT);
        super.setCommandListener(this);
        //addCommand(m_OpenCommand);
        //addCommand(m_SaveCommand);
        //setSelectCommand(m_OpenCommand);
        showList(FileSystemRegistry.listRoots(), true);
    }     
    public void setCommandListener(CommandListener cmd) {
        commandListener = cmd;
    }
    public CommandListener getCommandListener() {
        return commandListener;
    }
    //<editor-fold defaultstate="collapsed" desc=" Generated Methods ">//GEN-BEGIN:|methods|0|
    //</editor-fold>//GEN-END:|methods|0|
    //<editor-fold defaultstate="collapsed" desc=" Generated Method: initialize ">//GEN-BEGIN:|0-initialize|0|0-preInitialize
    /**
     * Initilizes the application.
     * It is called only once when the MIDlet is started. The method is called before the <code>startMIDlet</code> method.
     */
    private void initialize() {//GEN-END:|0-initialize|0|0-preInitialize
        // write pre-initialize user code here
//GEN-LINE:|0-initialize|1|0-postInitialize
        // write post-initialize user code here
    }//GEN-BEGIN:|0-initialize|2|
    //</editor-fold>//GEN-END:|0-initialize|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Method: switchDisplayable ">//GEN-BEGIN:|2-switchDisplayable|0|2-preSwitch
    /**
     * Switches a current displayable in a display. The <code>display</code> instance is taken from <code>getDisplay</code> method. This method is used by all actions in the design for switching displayable.
     * @param alert the Alert which is temporarily set to the display; if <code>null</code>, then <code>nextDisplayable</code> is set immediately
     * @param nextDisplayable the Displayable to be set
     */
    public void switchDisplayable(Alert alert, Displayable nextDisplayable) {//GEN-END:|2-switchDisplayable|0|2-preSwitch
        // write pre-switch user code here
        Display display = getDisplay();//GEN-BEGIN:|2-switchDisplayable|1|2-postSwitch
        if (alert == null) {
            display.setCurrent(nextDisplayable);
        } else {
            display.setCurrent(alert, nextDisplayable);
        }//GEN-END:|2-switchDisplayable|1|2-postSwitch
        // write post-switch user code here
    }//GEN-BEGIN:|2-switchDisplayable|2|
    //</editor-fold>//GEN-END:|2-switchDisplayable|2|

    /**
     * Returns a display instance.
     * @return the display instance.
     */
    public Display getDisplay() {
        return m_Display;
    }

    private void showList(Enumeration list, boolean toplevel) {
        this.deleteAll();
        if(!toplevel) append("..", null);
        for(;list.hasMoreElements();) {
            append((String)list.nextElement(), null);
        }
    }
    public FileConnection getSelectedFile() {
        return m_SelectedFile;
    }
    public String getCurURL() {
        if(m_CurPath == null) return null;
        return m_CurPath.getURL();
    }
    public FileConnection getCurFile() {
        return m_CurPath;
    }
    public void commandAction(Command c, Displayable d) {
        if(c == List.SELECT_COMMAND) {
            try {
                m_SelectedFile = null;
                if(m_CurPath == null) {
                    m_CurPath = (FileConnection) Connector.open("file:///" + getString(getSelectedIndex()));
                } else if(getString(getSelectedIndex()).equals("..")) {                    
                    try {
                        m_CurPath.setFileConnection(getString(getSelectedIndex()));
                    } catch(Exception e) {
                        m_CurPath = null;
                        showList(FileSystemRegistry.listRoots(), true);
                    }
                } else {
                    m_CurPath.setFileConnection(getString(getSelectedIndex()));
                    if(!m_CurPath.isDirectory()) {
                        m_SelectedFile = m_CurPath;
                        m_CurPath = (FileConnection) Connector.open("file://" + m_CurPath.getPath());
                        if(commandListener != null) {
                            commandListener.commandAction(FILE_SELECT_COMMAND, this);
                        }
                    }
                }
                showList(m_CurPath.list(), false);
            } catch(Exception e) {
                System.out.println("Exception " + e.toString());
            }
        } else if(commandListener != null) {
            commandListener.commandAction(c, d);
        }
    }
}
